package com.feidu.fragment;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockFragment;
import com.feidu.R;
import com.feidu.activity.CrimeListActivity;
import com.feidu.activity.RegisterActivity;
import com.feidu.bean.Person;
import com.feidu.em.EMHelperManager;
import com.feidu.utils.CommonUtil;
import com.feidu.utils.MessageID;
import com.feidu.utils.SharedPreferencesUtil;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * 生成日期：2016-7-11 <br/>
 * 描述：登录
 */
public class LoginFragment extends SherlockFragment {

	
	private EditText edit_name;
	private EditText edit_pword;
	private Button btn_login;
	
	private Button btn_register;
	
	private String name;
	private String pwd;
	
	public LoginFragment newInstance(){
		
		LoginFragment fragment = new LoginFragment();
		
		return fragment;
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_login, container,false);
		
		edit_name = (EditText)view.findViewById(R.id.edit_name);
		edit_pword = (EditText)view.findViewById(R.id.edit_pword);
		btn_login = (Button)view.findViewById(R.id.btn_login);
		btn_register = (Button)view.findViewById(R.id.btn_register);
		
		btn_login.setOnClickListener(onClickListener);
		btn_register.setOnClickListener(onClickListener);
		
		return view;
	}
	
	private static final String TAG = "LoginFragment";
	private ProgressDialog pd;
	
	private OnClickListener onClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.btn_login:
				name = edit_name.getText().toString().trim();
				pwd = edit_pword.getText().toString().trim();
				if (checkInfo(name, pwd)) {
					
					createDialog();
					EMLogin();
					String objectId = SharedPreferencesUtil.getUserObjId(getActivity());
					query(objectId);
				}
				
				break;
			case R.id.btn_register:
				register();
				break;
			}
		}
	};
	
	private boolean checkInfo(String name,String pwd){
		if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {
			
			CommonUtil.makeText(getActivity(),"请正确填写信息");
			
		}else{
			
			return true;
		}
		
		return false;
		
	}
	/**
	 * 创建Dialog
	 */
	private void createDialog(){

		pd = new ProgressDialog(getActivity());
		pd.setCanceledOnTouchOutside(false);
		pd.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {
				Log.d(TAG, "EMClient.getInstance().onCancel");
			}
		});
		pd.setMessage("登录中......");
		pd.show();
	}
	/**
	 * 登录环信
	 */
	private void EMLogin(){
		
		EMHelperManager.loginEM(getActivity(), name, pwd, mHandler);
	
	}
	
	/**
	 * 注册
	 */
	public void register() {
		startActivityForResult(new Intent(getActivity(),RegisterActivity.class), 0);
	}

	/**
	 * Bomb登录
	 * @param objectId
	 */
	private void query(String objectId){
		//查找Person表里面id为6b6c11c537的数据
		BmobQuery<Person> bmobQuery = new BmobQuery<Person>();
		bmobQuery.getObject(objectId,new QueryListener<Person>() {
			@Override
			public void done(Person person, BmobException e) {
				if(e==null){
					CommonUtil.makeText(getActivity(),  "登录成功"+person.getName());
					startActivity(new Intent(getActivity(), CrimeListActivity.class));
				}else{
					CommonUtil.makeText(getActivity(),  "登录失败,请注册"+e.toString());
				}
			}
		});
	}
	
	private Handler mHandler = new Handler(){
		
		public void handleMessage(android.os.Message msg) {
			
			switch (msg.what) {
			case MessageID.EM_LOGIN_SUCCESS:
				CommonUtil.makeText(getActivity(),"登录成功");
				Intent intent = new Intent(getActivity(),CrimeListActivity.class);
				startActivity(intent);
				pd.dismiss();
				break;
			case MessageID.EM_LOGIN_ERROR:
				int code = msg.arg1;
				CommonUtil.makeText(getActivity(),"登录失败："+code);
				pd.dismiss();
				break;
			}
		};
		
	};
	
	private void finish(){
		
		this.finish();
	}
}
